/*
 * Copyright 2007 National Bioinformatics Network
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package dgbgui.modeldocument.uimodels;

import javax.swing.DefaultComboBoxModel;

import dgbgui.modeldocument.definition.ClosureDefinition;

/**
 * @author bjnortier
 *
 */
@SuppressWarnings("serial")
public class ClosureTypeModel extends DefaultComboBoxModel {

    /**
     * Item for the display string int he combo box
     */
    private class ClosureTypeItem {
        
        public ClosureTypeItem(ClosureDefinition.Type type) {
            m_type = type;
        }
        
        public String toString() {
            return ClosureDefinition.closureTypeString(m_type);
        }
        
        public ClosureDefinition.Type getType() {
            return m_type;
        }
        
        ClosureDefinition.Type m_type;
    }
    
    /**
     * Default constructor. Add all the closure types
     */
    public ClosureTypeModel() {
        for ( ClosureDefinition.Type type : ClosureDefinition.Type.values() ) {
            ClosureTypeItem item = new ClosureTypeItem(type);
            addElement(item);
            if ( type == ClosureDefinition.Type.TRANSITIVE ) {
                setSelectedItem(item);
            }
        }
    }
    
    /**
     * Create the combo model, and set the current selected type
     * to the given type.
     * @param type The type to select
     */
    public ClosureTypeModel(ClosureDefinition.Type type) {
        this();
        for ( int k = 0; k < getSize(); ++k ) {
            if ( ((ClosureTypeItem)getElementAt(k)).getType() ==  type ) {
                setSelectedItem(getElementAt(k));
                break;
            }
        }
    }
    
    /**
     * @return The currently selected closure type
     */
    public ClosureDefinition.Type getSelected() {
        return ((ClosureTypeItem)getSelectedItem()).getType();
    }
    
}
